import {defineStore} from 'pinia'
import {getMenu} from '@/api/layout'
import router from '@/router'

export const useMenuStore = defineStore({
  id: 'menu',
  state: () => ({
    menuData: null
  }),
  actions: {
    // 初始化菜单数据
    async initMenuData() {
      let res = await getMenu()
      if (res.msg !== 'ok') return

      let addMenuComponent = (data: any, parent: string): any => {
        for (const item of data) {
          if (parent) {
            router.addRoute('layout', {
              path: `${parent}/${item.path}`,
              component: () => import(`../view${parent}/${item.path}/index.vue`)
            })
          }
          if (item.children)
            addMenuComponent(item.children, item.path)
        }
      }
      this.menuData = res.data
      addMenuComponent(res.data, '')
    }
  }
})